package com.gpay.erp.web.control;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gpay.erp.services.common.GlobalStatus;
import com.gpay.erp.services.system.OriginTypeService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.gpay.erp.core.dao.page.PageHelper;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.GoodsType;
import com.gpay.erp.core.system.bean.OriginType;
import com.gpay.erp.core.system.bean.TreeNode;
import com.gpay.erp.services.system.BasicInfoInteface;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;

/**
 * 基础信息管理 1.品种牌号管理 2.产地管理 3.仓库管理
 * 
 * @author lg
 */
@Controller
@RequestMapping("/basicInfoController/")
public class BasicInfoController extends BaseController {
	private Log log = LogFactory.getLog(BasicInfoController.class);
	@Autowired
	private BasicInfoInteface basicInfoService;
	@Autowired
	private GlobalStatus globalStatus;

	@Autowired
	private OriginTypeService originTypeService;

	/********************* 品种牌号管理 *****************************************/
	// 页面跳转 品种牌号管理
	@RequestMapping("goodTypeSelect")
	public String goodTypeSelect() {
		return "system/basicInfo/goodTypeSelect";
	}

	// 页面跳转 品种牌号新增
	@RequestMapping("goodTypeAdd")
	public String goodTypeAdd() {
		return "system/basicInfo/goodTypeAdd";
	}

	// 页面跳转 品种牌号修改
	@RequestMapping("goodTypeUpdate")
	public String goodTypeUpdate() {
		return "system/basicInfo/goodTypeUpdate";
	}

	/********************* 产地管理 *****************************************/
	// 页面跳转 产地管理
	@RequestMapping("originTypeSelect")
	public String originTypeSelect() {
		return "system/basicInfo/originTypeSelect";
	}

	// 页面跳转 产地新增
	@RequestMapping("originTypeAdd")
	public String originTypeAdd() {
		return "system/basicInfo/originTypeAdd";
	}

	// 页面跳转 产地修改
	@RequestMapping("originTypeUpdate")
	public String originTypeUpdate() {
		return "system/basicInfo/originTypeUpdate";
	}


	/*
 * 点击新增，增加产地信息
 *
 * @param originType
 *
 * @param request
 *
 * @param response
 */
	@RequestMapping("selectListOriginType")
	public void selectListOriginType(OriginType originType,
							  HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();

		List<OriginType> originTypeList = originTypeService.selectList(originType);// 执行新增操作
		if (originTypeList != null) {
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("select.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(originTypeList);
		WebUtil.responseToJson(response, responseInfo);
	}
	// 产地状态
	@RequestMapping("originGlobalStatus")
	public void originGlobalStatus(HttpServletRequest request,
			HttpServletResponse response) {
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setResult("00010");
		responseInfo.setMessage("select.success");
		responseInfo.setObject(globalStatus.getOrgState());// 获取产地状态，传递给页面
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击树型菜单--产地管理，进行分页查询
	 * 
	 * @param pageHelper
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("selectOriginType")
	public void selectOriginType(PagerModel pagerModel, OriginType originType,
			PageHelper pageHelper, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		int pagenow = pageHelper.getPage();// 当前页
		int rows = pageHelper.getRows();// 每页显示记录数
		originType.setPageSize(rows);
		originType.setFromRow((pagenow - 1) * rows);
		originType.setToRow(pagenow * rows);
		pagerModel = basicInfoService.selectPageList(originType); // 分页查询，返回PagerModel
		log.info("selectOriginType end");
		if (pagerModel.getRows().size() > 0) {
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击新增，增加产地信息
	 * 
	 * @param originType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("addOriginType")
	public void addOriginType(OriginType originType,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/*
		 * originType.setCode("XA"); originType.setCountry("武汉");
		 * originType.setOrNo("p0001"); originType.setOrName("夏安");
		 * originType.setState("1");
		 */
		int flag = basicInfoService.insert(originType);// 执行新增操作
		if (flag != 0) {
			log.info("t_origin_type表插入行的id值为：" + flag);
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("add.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("add.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击明细，根据ID查看产地详细信息
	 * 
	 * @param originType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("detailOriginType")
	public void detailOriginType(PagerModel pagerModel, OriginType originType,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* originType.setId("2"); */
		if (originType.getId() != null)// 获取前端传入的id,判断是否为null
		{
			pagerModel = basicInfoService.selectById(originType.getId());// 根据ID查询明细
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		} else {
			responseInfo.setResult("00011");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，修改产地信息
	 * 
	 * @param bankCard
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("updateOriginType")
	public void updateOriginType(OriginType originType,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/*
		 * originType.setId("1"); originType.setOrNo("p0010");
		 */
		if (originType.getId() != null) {
			int id = basicInfoService.update(originType);// 执行修改操作
			if (id != 0) {
				log.info("修改记录的id值为：" + id);
				responseInfo.setResult("00030");
				responseInfo.setMessage(requestContext
						.getMessage("update.success"));
			} else {
				responseInfo.setResult("00031");
				responseInfo.setMessage(requestContext
						.getMessage("update.fail"));
			}
		} else {
			responseInfo.setResult("00031");
			responseInfo.setMessage(requestContext.getMessage("update.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击删除，根据id删除产地信息
	 * 
	 * @param ids[]
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("deleteOriginType")
	public void deleteOriginType(HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		String[] ids = request.getParameterValues("ids");// 接收前端传输的id值
		// String [] ids={"2"};
		if (ids != null) {
			int dels = basicInfoService.deletes(ids);// 执行删除操作，删除成功返回1，失败返回0
			if (dels != 0) {
				responseInfo.setResult("00020");
				responseInfo.setMessage(requestContext
						.getMessage("delete.success"));
			} else {
				responseInfo.setResult("00021");
				responseInfo.setMessage(requestContext
						.getMessage("delete.fail"));
			}
		} else {
			responseInfo.setResult("00021");
			responseInfo.setMessage(requestContext.getMessage("delete.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 品种牌号信息树结构
	 * 
	 * @param request
	 * 
	 * @param response
	 */

	@RequestMapping("goodsTree")
	public void tree(GoodsType goodsType, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		List<TreeNode> list = TreeNode.formatTree(basicInfoService
				.selectList(goodsType));// 执行查询品种菜单信息，放入list集合
		if (list.size() > 0) {
			responseInfo.setResult("13000");
			responseInfo.setMessage(requestContext
					.getMessage("goods.menu.success"));
			responseInfo.setObject(list);
		} else {
			responseInfo.setResult("13001");
			responseInfo.setMessage(requestContext
					.getMessage("goods.menu.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击明细，根据ID查看品种详细信息
	 * 
	 * @param goodsType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("detailGoodsType")
	public void detailGoodsType(PagerModel pagerModel, GoodsType goodsType,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/* goodsType.setId("6"); */
		if (goodsType.getId() != null)// 获取前端传入的id,判断是否为null
		{
			pagerModel = basicInfoService.selectOne(goodsType);// 根据ID查询明细
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		} else {
			responseInfo.setResult("00011");
			responseInfo.setMessage(requestContext.getMessage("select.fail"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击新增，增加品种信息
	 * 
	 * @param goodsType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("addGoodsType")
	public void addGoodsType(GoodsType goodsType, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/*
		 * goodsType.setNodeLevel("2");//节点层次 goodsType.setPid("4"); //父节点
		 * goodsType.setTypeName("热轧硅钢");//节点名
		 * goodsType.setGoodsCode("DA10");//节点代码
		 */
		int id = basicInfoService.insertGoods(goodsType);// 执行新增操作
		if (id != 0) {
			log.info("t_goods_type表插入行的id值为：" + id);
			responseInfo.setResult("00040");
			responseInfo.setMessage(requestContext.getMessage("add.success"));
		} else {
			responseInfo.setResult("00041");
			responseInfo.setMessage(requestContext.getMessage("add.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，修改品种信息
	 * 
	 * @param goodsType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("updateGoodsType")
	public void updateGoodsType(GoodsType goodsType,
			HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		/*
		 * goodsType.setId("20"); goodsType.setTypeName("冷轧硅钢");
		 */
		if (goodsType.getId() != null) {
			int id = basicInfoService.updateGoods(goodsType);// 执行修改操作
			if (id != 0) {
				log.info("修改记录的id值为：" + id);
				responseInfo.setResult("00030");
				responseInfo.setMessage(requestContext
						.getMessage("update.success"));
			} else {
				responseInfo.setResult("00031");
				responseInfo.setMessage(requestContext
						.getMessage("update.fail"));
			}
		} else {
			responseInfo.setResult("00031");
			responseInfo.setMessage(requestContext.getMessage("update.fail"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	/*
	 * 点击修改，品种信息下拉框
	 * 
	 * @param goodsType
	 * 
	 * @param request
	 * 
	 * @param response
	 */
	@RequestMapping("goodsTypeList")
	public void goodsTypeList(PagerModel pagerModel, GoodsType goodsType,
			PageHelper pageHelper, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		int pagenow = pageHelper.getPage();// 当前页
		int rows = pageHelper.getRows();// 每页显示记录数
		goodsType.setPageSize(rows);
		goodsType.setFromRow((pagenow - 1) * rows);
		goodsType.setToRow(pagenow * rows);
		goodsType.setNodeLevel("3");// 获取最底层品种数据
		pagerModel = basicInfoService.selectPageList1(goodsType); // 分页查询，返回PagerModel
		log.info("selectGoodsType end");
		if (pagerModel.getRows().size() > 0) {
			responseInfo.setResult("00010");
			responseInfo
					.setMessage(requestContext.getMessage("select.success"));
		}
		responseInfo.setObject(pagerModel);
		WebUtil.responseToJson(response, responseInfo);
	}
}
